home *** CD-ROM | disk | FTP | other *** search
- /*** AEVT.c ***/
-
- /*
- This is an ugly bit of code, hacked together to demonstrate how an application
- can handle the custom event for MacHTTP searches (WWWΩsrch in AppleScript terms).
-
- The only 3 routines of any interest are "HandleSRCH", "HandleSDOC", and "InitAppleEvents."
- They are near the end of this source file. Everything else here is for handling
- the required suite of events.
-
- The two custom AppleEvents handled are:
-
- Search: Receive a search argument from MacHTTP and return results --ex. Search "some text"
- Search 'char' -- Search arguments
- Result: 'char' -- Search Results
-
- Search Doc: Search a specific document for keywords
- Search Doc 'char' -- Path Arguments (usually the document to be searched)
- for 'char' -- Search arguments
- [address 'char'] -- Client Address
- [user 'char'] -- User Name
- [password 'char'] -- Password
- Result: 'char' -- Search Results
-
- */
-
- #include <stdio.h>
- #include <string.h>
- #include <AppleEvents.h>
-
- #include <Types.h>
- #include <Memory.h>
- #include <Windows.h>
- #include <Dialogs.h>
- #include <Menus.h>
- #include <Fonts.h>
- #include <Events.h>
- #include <OSEvents.h>
- #include <Desk.h>
- #include <ToolUtils.h>
-
- #include "AEVT.h"
- #include "TTY_Messages.h"
- #include "Search.h"
-
- extern int done; /*defined in Main.c*/
-
- /*******************************************************/
-
- OSErr GotRequiredParams( AppleEvent *theAppleEvent )
- {
- DescType typeCode;
- Size actualSize;
- OSErr err;
-
- err = AEGetAttributePtr(theAppleEvent, keyMissedKeywordAttr,
- typeWildCard, &typeCode, nil, 0, &actualSize) ;
- if (err == errAEDescNotFound)
- return noErr;
- else if (err == noErr)
- return errAEEventNotHandled;
- else
- return err;
- } /*GotRequiredParams*/
-
-
-
- /*******************************************************/
-
- /*There are no parameters for an open application event. So all we need do is open
- an untitled window. If a reply is sought, the default reply will do.
- Note that this message will normally come only from the Finder */
-
- pascal OSErr HandleOAPP( AppleEvent *theAppleEvent, AppleEvent *reply, long handlerRefcon)
- {
- OSErr err;
- TTY_WriteMessage("Open Event");
- /* We don't expect any params at all, but check in case the client requires any */
- err = GotRequiredParams( theAppleEvent ) ;
- if (err != noErr)
- return err;
- else {
- /* Perform open doc ation here*/
- return noErr;
- }
- }
-
- /*******************************************************/
-
- FailErr(OSErr err, char *a)
- {
- char s[40];
- if (err) {
- sprintf(s, "Error %d: %s\r", err, a);
- TTY_WriteMessage(s);
- }
- }
-
- /*******************************************************/
-
- pascal short MyWaitReplyIdleProc(EventRecord *event, RgnHandle mouseRgn,long sleeptime )
- {
- return FALSE;
- }
-
- /*******************************************************/
-
- pascal OSErr HandleQUIT( AppleEvent *theAppleEvent, AppleEvent *reply, long handlerRefcon)
- {
- OSErr err;
- char *errStr;
- short willQuit; /* did the user allow the quit or cancel */
-
-
- TTY_WriteMessage("Quit Event");
- /* We don't expect any params at all, but check in case the client requires any */
- FailErr( GotRequiredParams( theAppleEvent ), "parms") ;
-
- done = 1;
- if (reply->dataHandle != nil) { /* a reply is sought */
-
- errStr = "yessir; about to quit, sir";
-
- return AEPutParamPtr( reply, 'errs', 'TEXT', errStr, strlen(errStr) );
- }
- else {
- return noErr;
- }
- }
-
-
- /*******************************************************/
-
- pascal OSErr HandleODOC( AppleEvent *theAppleEvent, AppleEvent *reply, long handlerRefcon)
- {
- FSSpec myFSS;
- AEDescList docList;
- long index, itemsInList;
- Size actualSize;
- AEKeyword keywd;
- DescType typeCode;
- WindowPtr ignoreWPtr;
- char s[80],t[80];
-
- /* we are expecting a list of aliases: first get the list into theDesc and check
- that it is the only param the sender considers essential */
- TTY_WriteMessage("Open Doc Event");
-
- FailErr( AEGetParamDesc( theAppleEvent, keyDirectObject, typeAEList, &docList ), "desc" ) ;
- FailErr( GotRequiredParams( theAppleEvent ), "parms" ) ;
-
- /* now get each alias from the list (as an FSSSpec) and open the associated file. */
-
- FailErr( AECountItems( &docList, &itemsInList ), "count" ) ;
-
- for (index = 1; index<=itemsInList; index++) {
- FailErr( AEGetNthPtr( &docList, index, typeFSS, &keywd, &typeCode, /*coercion does alias->fsspec */
- (Ptr) &myFSS, sizeof( myFSS ), &actualSize ), "NthPtr") ;
-
- /** Do Open file stuff ignoreWPtr = OpenFile( &myFSS ) ;**/
- strncpy(t, (char *)&(myFSS.name[1]), myFSS.name[0]);
- t[myFSS.name[0]] = '\0';
- sprintf(s,"Open %s\r",t);
- TTY_WriteMessage(s);
- }
-
-
- FailErr( AEDisposeDesc( &docList ), "dispose" ) ;
- return noErr ;
- } /*HandleODOC */
-
-
- /*******************************************************/
-
- pascal OSErr HandlePDOC( AppleEvent *theAppleEvent, AppleEvent *reply, long handlerRefcon)
- {
- FSSpec myFSS;
- AEDescList docList;
- long index, itemsInList;
- Size actSize;
- AEKeyword keywd;
- DescType typeCode;
- WindowPtr ignoreWPtr;
- OSErr err;
- AEDesc ignoreDesc;
- short ignore;
-
- TTY_WriteMessage("Print Doc Event");
- /* we are expecting a list of aliases: first get the list into docList */
-
- FailErr( AEGetParamDesc( theAppleEvent, keyDirectObject, typeAEList, &docList ), "desc" ) ;
-
- /* now check that all required params have been used: return error if there are any
- that we don't understand */
-
- FailErr( GotRequiredParams( theAppleEvent ), "parms") ;
-
- /* now get each alias (as FSSpec) from the list and open it. */
-
- FailErr( AECountItems( &docList, &itemsInList ), "count" ) ;
-
- for (index = 1; index<=itemsInList; index++) {
-
- FailErr( AEGetNthPtr( &docList, index, typeFSS, &keywd, &typeCode,
- (Ptr) &myFSS, sizeof( myFSS ), &actSize ), "Nth" ) ;
-
- /*** Print stuff here PrintFile( &myFSS ) ;**/
- }
- FailErr( AEDisposeDesc( &docList ), "Dispose" ) ;
-
- return noErr;
- }
-
- /***********************************************/
- /* This routine handles the incoming WWWΩsrch AppleEvent */
-
- pascal OSErr HandleSRCH( AppleEvent *theAppleEvent, AppleEvent *reply, long handlerRefcon)
- {
- Size actSize;
- DescType typeCode;
- char s[1024], t[1024];
-
- TTY_WriteMessage("WWWΩsrch AppleEvent");
-
- /* Get the single direct parameter for this event */
-
- FailErr( AEGetParamPtr( theAppleEvent, keyDirectObject, typeChar, &typeCode,
- (Ptr) s, sizeof( s ), &actSize ), "arg 1" ) ;
-
- /*pretty up the string and show it to the user on the screen*/
- s[actSize] = '\0';
- sprintf(t, "Search Args: %s",s);
- TTY_WriteMessage(t);
-
- /**************************************************************/
- /*This is where any processing, searching, etc. should happen.*/
- sprintf (t, "<h1>%s</h1> This is what you sent!<p>", s);
-
- /*return the result of any processing to the caller (MacHTTP)*/
- if (reply->descriptorType != typeNull)
- FailErr( AEPutParamPtr(reply, keyDirectObject, typeChar,
- t, strlen (t) ), "Reply");
-
- return noErr;
- }
-
- /***********************************************/
- /* This routine handles the incoming WWWΩsdoc AppleEvent */
-
- pascal OSErr HandleSDOC( AppleEvent *theAppleEvent, AppleEvent *reply, long handlerRefcon)
- {
- Size actSize;
- DescType typeCode;
- char s[1024], t[1024], out[1024];
- char host[256], user[256], pass[256];
- char *data;
- OSErr err;
-
- TTY_WriteMessage("WWWΩsdoc AppleEvent");
-
- /* Get the direct parameter for this event (path args) */
- FailErr( AEGetParamPtr( theAppleEvent, keyDirectObject, typeChar, &typeCode,
- (Ptr) s, sizeof( s ), &actSize ), "sdoc arg 1" ) ;
-
- s[actSize] = '\0';
-
- /* Get the "for" parameter for this event (search args) */
- FailErr( AEGetParamPtr( theAppleEvent, kForKeyword, typeChar, &typeCode,
- (Ptr) t, sizeof( t ), &actSize ), "sdoc arg 2" ) ;
-
- t[actSize] = '\0';
-
- /**********************************************************/
- /* Note that the following 3 parameters, address, user name, and password,
- * are optional event parameters and may not always be present. If these
- * parameters are missing, the following code initializes them to the null
- * string.
- */
-
- /* Get the "address" parameter for this event*/
- if (err=AEGetParamPtr( theAppleEvent, kAddressKeyword, typeChar, &typeCode,
- (Ptr) host, sizeof( host ), &actSize ))
- actSize = 0;
-
- host[actSize] = '\0';
-
- /* Get the "user name" parameter for this event*/
- if (err=AEGetParamPtr( theAppleEvent, kUserKeyword, typeChar, &typeCode,
- (Ptr) user, sizeof( user ), &actSize ))
- actSize = 0;
-
- user[actSize] = '\0';
-
- /* Get the "password" parameter for this event*/
- if (err=AEGetParamPtr( theAppleEvent, kPasswordKeyword, typeChar, &typeCode,
- (Ptr) pass, sizeof( pass ), &actSize ))
- actSize = 0;
-
- pass[actSize] = '\0';
-
- /*pretty up the strings and show them to the user on the screen*/
- sprintf(out, "Path Args: %s, Search Args: %s, address: %s, user: %s, password: %s", s, t,
- host, user, pass);
- TTY_WriteMessage(out);
-
- /**************************************************************/
- /*This is where any processing, searching, etc. should happen.*/
- /*any security checks based on address, user name, and password may
- * be performed here as well.
- */
- sprintf (out, "<h1>Searching \"%s\" for \"%s\" </h1> This is what you sent!<p>", s, t);
-
- if (data = (char *) NewPtr (32768)) {
-
- if (SearchDocument (s, t, data))
- sprintf (data, "Error, unable to search %s", s);
-
- /*return the result of any processing to the caller (MacHTTP)*/
- if (reply->descriptorType != typeNull)
- FailErr( AEPutParamPtr(reply, keyDirectObject, typeChar,
- data, strlen (data) ), "sdoc Reply");
-
- DisposePtr(data);
-
- return noErr;
- }
- else {
- TTY_WriteMessage ("Error: Unable to allocate memory for search data buffer");
- return (-1);
- }
- }
-
- /***********************************************/
- OSErr InitAppleEvents()
- {
- OSErr aevtErr;
- aevtErr = noErr ;
-
- /*Install handlers for required events*/
- if ( aevtErr == noErr )
- aevtErr = AEInstallEventHandler( kCoreEventClass, kAEQuitApplication,
- HandleQUIT, 0, false ) ;
- if ( aevtErr == noErr )
- aevtErr = AEInstallEventHandler( kCoreEventClass, kAEOpenApplication,
- HandleOAPP, 0, false ) ;
- if ( aevtErr == noErr )
- aevtErr = AEInstallEventHandler( kCoreEventClass, kAEOpenDocuments,
- HandleODOC, 0, false ) ;
- if ( aevtErr == noErr )
- aevtErr = AEInstallEventHandler( kCoreEventClass, kAEPrintDocuments,
- HandlePDOC, 0, false ) ;
-
- /*install a handler for the MacHTTP search event*/
- if ( aevtErr == noErr )
- aevtErr = AEInstallEventHandler( kMyEventClass, kMyAEWWWSearch,
- HandleSRCH, 0, false ) ;
-
- /*install a handler for the MacHTTP search doc event*/
- if ( aevtErr == noErr )
- aevtErr = AEInstallEventHandler( kMyEventClass, kMyAEWWWSearchDoc,
- HandleSDOC, 0, false ) ;
-
- TTY_WriteMessage("Apple Events initialized.\r");
- return aevtErr;
- }